<?php

namespace Home\Controller;

use Utuo\ForegroundController;
use My\Model\ProductOrderModel;

class ProductController extends ForegroundController {
	
	// 查询订单状态
	private $types = array(
			1 => "未付款订单",
			2 => "已付款订单",
			4 => "货到付款订单",
			3 => "交易成功订单",
			0 => "其它订单" 
	);
	
	// 初始化
	public function _init() {
		layout('product');
		$this->setModelName();
	}
	
	// 首页
	public function indexAction() {
		$this->setPageTitle($this->config['name']);
		$this->display();
	}
	
	// 扩展信息
	public function configAction() {
		$id = I('id', 0, 'intval');
		$config = M('ProductConfig')->where("cid=%d AND id=%d", $this->sid, $id)->find();
		$this->assign('config', $config);
		$this->display();
	}
	
	// 获取目录
	private function getCategory() {
		$this->catId = $id = I('id', 0, 'intval');
		$map['id'] = $id;
		$map['cid'] = $this->sid;
		$map['status'] = 1;
		$map['is_deleted'] = 0;
		$category = M('ProductCategory')->where($map)->find();
		$this->assign('category', $category);
		$this->setPageTitle($category['name'], $this->config['name']);
		$this->assign('cat_id', $id);
	}
	
	// 商品栏目列表
	public function cateAction() {
		$this->getCategory();
		$this->display();
	}
	
	// 商品列表
	public function listAction() {
		$this->getCategory();
		$this->display();
	}
	
	// 商品详情页
	public function showAction() {
		$id = I('id', 0, 'intval');
		$map['pdt.cid'] = $this->sid;
		$map['pdt.status'] = 1;
		$map['pdt.is_deleted'] = 0;
		$map['pdt.id'] = $id;
		$dProductGoods = D("My/ProductGoods", "Model");
		$productGoods = M('ProductGoods pdt')->where($map)->join("__PRODUCT_GOODS_CONFIG__ cfg on cfg.id=pdt.id", 'LEFT')->find();
		$productGoods['attributes'] = $dProductGoods->getAttributesInfo($productGoods['attribute_values'], $id); // 获得属性信息
		$productGoods['shipping_cfg'] = $dProductGoods->getShippingWay($productGoods); // 获得送费标准
		//$productGoods['pic'] = getImageUrl($productGoods['pic'], "show");
		$this->setPageTitle( $productGoods['name'], $this->config['name']);
		$this->assign('productGoods', $productGoods);
		$this->display();
	}
	
	/**
	 * 个人中心
	 */
	public function myAction() {
		$this->checkLogin();
		$this->getOrderNav();
		$this->setPageTitle('用户中心');
		$user = M("User")->find($this->uid);
		$this->assign("user", $user);
		$this->display();
	}
	
	/**
	 * 信息修改
	 */
	public function infoModifyAction() {
		$user = M("User")->find($this->uid);
		$this->assign("user", $user);
		$this->display();
	}
	
	// 订单导航
	private function getOrderNav() {
		$types = array();
		foreach (ProductOrderModel::$TYPES as $type => $v) {
			$condition = array(
					"uid" => $this->uid,
					"cid" => $this->sid 
			);
			if ($type == 1) { // 未付款
				$condition['order_status'] = ProductOrderModel::ORDER_STATUS_PAY;
			} else if ($type == 2) { // 待签收(已付款,货到付款)
				$condition['order_status'] = array(
						"in",
						array(
								ProductOrderModel::ORDER_STATUS_SHIPPING,
								ProductOrderModel::ORDER_STATUS_SIGN 
						) 
				);
			} else if ($type == 3) { // 交易成功
				$condition['order_status'] = ProductOrderModel::ORDER_STATUS_COMPLETED;
			} else { // 其它
				$condition['order_status'] = array(
						"not in",
						array(
								ProductOrderModel::ORDER_STATUS_PAY,
								ProductOrderModel::ORDER_STATUS_SHIPPING,
								ProductOrderModel::ORDER_STATUS_SIGN,
								ProductOrderModel::ORDER_STATUS_COMPLETED 
						) 
				);
			}
			$count = M("ProductOrder")->where($condition)->count();
			$types[$type] = $v . "($count)";
		}
		$this->assign("types", $types);
	}
	
	// 更多商品
	public function moreAction() {
		$all = I("all", 1) and $this->getCategory(); // 是否分栏目(默认分)
		$type = I("type", 0); // 排序 0.最新 1.销量
		$type == 1 and $order = "saled_count desc"; // 销量
		$list = $this->lists($all ? $this->catId : null, true, $this->getPageSize(), $this->getPage(), $order);
		$totalPages = ceil($list['count'] / $this->getPageSize()); // 总页数
		                                                           // 显示更多文章数据
		$data = array();
		if ($this->getPage() <= $totalPages) {
			$data = $list['data'];
			foreach ($data as &$v)
				$v['url'] = get_url($this->sid, 'Product', 'show', "id={$v['id']}");
		}
		$this->ajaxSuccess('', $data);
	}
	
	// 排行
	public function productRankAction() {
		$rankName = array(
				0 => '新品排行榜',
				1 => '热销排行榜' 
		);
		$type = I("type", 0); // 排序 0.最新 1.销量
		$type == 1 and $order = "saled_count desc"; // 销量
		$data = $this->lists(null, true, $this->getPageSize(), $this->getPage(), $order);
		$productGoodsLists = $data["data"];
		foreach ($productGoodsLists as &$v)
			$v['url'] = get_url($this->sid, 'Product', 'show', "id={$v['id']}");
		$this->assign("productGoodsLists", $productGoodsLists);
		$this->assign('rankName', $rankName[$type]);
		$this->display();
	}
	
	// 商品查询
	public function searchAction() {
		$keywords = I("keywords", "", "strip_tags");
		$condition = array();
		if ($keywords)
			$condition["name"] = array(
					"like",
					"%$keywords%" 
			);
		$productLists = $this->lists(null, true, $this->getPageSize(), $this->getPage(), null, $condition);
		foreach ($productLists["data"] as &$v)
			$v['url'] = get_url($this->sid, 'Product', 'show', "id={$v['id']}");
		if ( IS_AJAX ) {
			$this->ajaxSuccess('', array(
				'page' => $this->getPage(),
				'totalPage' => ceil( $productLists['count'] / $this->getPageSize() ),
				'totalRows' => $productLists['count'],
				'data' => $productLists["data"],
			));
		} else {
			$this->assign("productLists", $productLists);
			$this->assign("keywords", $keywords);
			$this->display("Product/productSearchShow");
		}
	}
	
	// 获得商品列表
	private function lists($cateId = 0, $paginate = false, $pageSize = 10, $p = 1, $order = "id desc", $wparam = array()) {
		$order or $order = "id desc";
		$condition = array(
				"cid" => $this->sid,
				"is_deleted" => 0,
				"status" => 1 
		);
		$condition = array_merge($condition, $wparam);
		$cateId !== null and $condition['cat_id'] = $cateId;
		$m = M("ProductGoods")->where($condition);
		$new_m = clone $m;
		// 分页处理
		if ($paginate) {
			$m->limit($this->getPageSize())->page($this->getPage());
			$contentCount = $new_m->count();
		}
		$contentLists = $m->order("$order")->select();
		return array(
				'count' => $contentCount,
				'data' => $contentLists 
		);
	}
	public function getProductInfoAction($cid){
		$condations=array(
			'pc.is_deleted'=>0,
			'pc.status'=>1,
			'p.cid'=>$cid		
		);
		$m=M('Product  p' );
		$m->join('__PRODUCT_CONFIG__ pc on p.cid=pc.cid');
		$m->field('p.tel,pc.content');
		$info=$m->where($condations)->select();
	    $array=array();
	    $array['tel']=$info[0]['tel'];
	    foreach ($info as $v)
	    {
	    	$array['content'].=$v['content'];
	    }
	   
		$this->ajaxReturn($array);
	}
	
	
	
}